﻿using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace JX.Dapper
{
    /// <summary>
    /// 数据操作上下文。
    /// </summary>
	public class MyDapperDBContext : DapperDBContext
	{
        /// <summary>
        /// 构造器注入
        /// </summary>
        /// <param name="optionsAccessor"></param>
        public MyDapperDBContext(IOptions<DapperDBContextOptions> optionsAccessor) : base(optionsAccessor)
        {
        }

        /// <summary>
        /// 创建数据库连接类
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="providerName">数据库提供者类型：sqlserver、mysql</param>
        /// <returns></returns>
        protected override IDbConnection CreateConnection(string connectionString, string providerName = "sqlserver")
        {
            IDbConnection conn;
            switch (providerName.ToLower())
            {
                case "sqlserver":
                    conn = new System.Data.SqlClient.SqlConnection(connectionString);
                    break;
                //case "mysql":
                //    conn = new System.Data.SqlClient.SqlConnection(connectionString);
                //    break;
                default:
                    conn = new System.Data.SqlClient.SqlConnection(connectionString);
                    break;
            }
            return conn;
        }
    }
}
